#######################################
##      Replicating Tables 3-5       ##
##         Other Scandals            ##
##   in Hamel and Miller (2018)      ##
#######################################

install.packages(c("Matching", "rgenoud"))
library(Matching)
library(rgenoud)

scandal <- read.csv("scandal_dta.csv")

## All Other Scandals

other_scandal <- subset(scandal, other == 1)

attach(other_scandal)

covariates_match <- cbind(break_id, dem, female, prev_votepct, dwnom1, seniority,
                          les, dpres, logged_pre_sum_infl)

covariates_balance <- cbind(break_id, dem, female, prev_votepct, dwnom1, seniority,
                            les, dpres, logged_pre_sum_infl)

match_other <- GenMatch(Tr = scandal_id, X = covariates_match, 
                            BalanceMatrix = covariates_balance, M = 3, 
                            pop.size = 12500, max.generations = 10, wait.generations = 5, 
                            hard.generation.limit = FALSE, int.seed = 833, unif.seed = 149, 
                            fit.func = "pvals", exact = as.logical(c(1,1,1,0,0,0,0,0,0)), 
                            replace = TRUE, ties = FALSE)

effect_other_vote <- Match(Y = votepct, Tr = scandal_id, 
                               X = covariates_match, M = 3, 
                               exact = as.logical(c(1,1,1,0,0,0,0,0,0)), 
                               replace = TRUE, ties = FALSE, version = "fast", 
                               Weight.matrix = match_other)
summary(effect_other_vote)

effect_other_win <- Match(Y = win, Tr = scandal_id, 
                            X = covariates_match, M = 3, 
                            exact = as.logical(c(1,1,1,0,0,0,0,0,0)), 
                            replace = TRUE, ties = FALSE, version = "fast", 
                            Weight.matrix = match_other, 
                            match.out = effect_other_vote)
summary(effect_other_win)

effect_other_money <- Match(Y = logged_post_sum_infl, Tr = scandal_id, 
                                X = covariates_match, M = 3, 
                                exact = as.logical(c(1,1,1,0,0,0,0,0,0)), 
                                replace = TRUE, ties = FALSE, version = "fast", 
                                Weight.matrix = match_other, 
                                match.out = effect_other_vote)
summary(effect_other_money)

balance_other <- MatchBalance(scandal_id ~ break_id + dem + female + prev_votepct + 
                                    dwnom1 + seniority + les + dpres + 
                                    logged_pre_sum_infl + chall_quality, 
                                  match.out = effect_other_vote,
                                  nboots = 5000)

detach(other_scandal)

## Covered Other Scandals

other_scandal_covered <- subset(scandal, other == 1 & covered == 1)

attach(other_scandal_covered)

covariates_match <- cbind(break_id, dem, female, prev_votepct, dwnom1, seniority,
                          les, dpres, logged_pre_sum_infl)

covariates_balance <- cbind(break_id, dem, female, prev_votepct, dwnom1, seniority,
                            les, dpres, logged_pre_sum_infl)

match_other_covered <- GenMatch(Tr = scandal_id, X = covariates_match, 
                                    BalanceMatrix = covariates_balance, M = 3, 
                                    pop.size = 12500, max.generations = 10, wait.generations = 5, 
                                    hard.generation.limit = FALSE, int.seed = 641, unif.seed = 972, 
                                    fit.func = "pvals", exact = as.logical(c(1,1,1,0,0,0,0,0,0)), 
                                    replace = TRUE, ties = FALSE)

effect_other_vote_covered <- Match(Y = votepct, Tr = scandal_id, 
                                       X = covariates_match, M = 3, 
                                       exact = as.logical(c(1,1,1,0,0,0,0,0,0)), 
                                       replace = TRUE, ties = FALSE, version = "fast", 
                                       Weight.matrix = match_other_covered)
summary(effect_other_vote_covered)

effect_other_win_covered <- Match(Y = win, Tr = scandal_id, 
                                    X = covariates_match, M = 3, 
                                    exact = as.logical(c(1,1,1,0,0,0,0,0,0)), 
                                    replace = TRUE, ties = FALSE, version = "fast", 
                                    Weight.matrix = match_other_covered, 
                                    match.out = effect_other_vote_covered)
summary(effect_other_win_covered)

effect_other_money_covered <- Match(Y = logged_post_sum_infl, Tr = scandal_id, 
                                        X = covariates_match, M = 3, 
                                        exact = as.logical(c(1,1,1,0,0,0,0,0,0)), 
                                        replace = TRUE, ties = FALSE, version = "fast", 
                                        Weight.matrix = match_other_covered, 
                                        match.out = effect_other_vote_covered)
summary(effect_other_money_covered)

balance_other_covered <- MatchBalance(scandal_id ~ break_id + dem + female + prev_votepct + 
                                            dwnom1 + seniority + les + dpres + 
                                            logged_pre_sum_infl + chall_quality, 
                                          match.out = effect_other_vote_covered,
                                          nboots = 5000)
detach(other_scandal_covered)

## Non-Covered Other Scandals

other_scandal_noncovered <- subset(scandal, other == 1 & covered == 0)

attach(other_scandal_noncovered)

covariates_match <- cbind(break_id, dem, female, prev_votepct, dwnom1, seniority,
                          les, dpres, logged_pre_sum_infl)

covariates_balance <- cbind(break_id, dem, female, prev_votepct, dwnom1, seniority,
                            les, dpres, logged_pre_sum_infl)

match_other_noncovered <- GenMatch(Tr = scandal_id, X = covariates_match, 
                                       BalanceMatrix = covariates_balance, M = 3, 
                                       pop.size = 12500, max.generations = 10, wait.generations = 5, 
                                       hard.generation.limit = FALSE, int.seed = 473, unif.seed = 155, 
                                       fit.func = "pvals", exact = as.logical(c(1,1,1,0,0,0,0,0,0)), 
                                       replace = TRUE, ties = FALSE)

effect_other_vote_noncovered <- Match(Y = votepct, Tr = scandal_id, 
                                          X = covariates_match, M = 3, 
                                          exact = as.logical(c(1,1,1,0,0,0,0,0,0)), 
                                          replace = TRUE, ties = FALSE, version = "fast", 
                                          Weight.matrix = match_other_noncovered)
summary(effect_other_vote_noncovered)

effect_other_win_noncovered <- Match(Y = win, Tr = scandal_id, 
                                       X = covariates_match, M = 3, 
                                       exact = as.logical(c(1,1,1,0,0,0,0,0,0)), 
                                       replace = TRUE, ties = FALSE, version = "fast", 
                                       Weight.matrix = match_other_noncovered, 
                                       match.out = effect_other_vote_noncovered)
summary(effect_other_win_noncovered)

effect_other_money_noncovered <- Match(Y = logged_post_sum_infl, Tr = scandal_id, 
                                           X = covariates_match, M = 3, 
                                           exact = as.logical(c(1,1,1,0,0,0,0,0,0)), 
                                           replace = TRUE, ties = FALSE, version = "fast", 
                                           Weight.matrix = match_other_noncovered, 
                                           match.out = effect_other_vote_noncovered)
summary(effect_other_money_noncovered)

balance_other_noncovered <- MatchBalance(scandal_id ~ break_id + dem + female + prev_votepct + 
                                               dwnom1 + seniority + les + dpres + 
                                               logged_pre_sum_infl + chall_quality, 
                                             match.out = effect_other_vote_noncovered,
                                             nboots = 5000)

detach(other_scandal_noncovered)
